import { asyncRouterMap, constantRouterMap } from '@/router'
import { getLoginType } from '@/utils/auth'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
function hasPermission(roles, route, enableModules, isStaff) {
  if (route.meta) {
    if (route.meta.roles) {
      if (!roles.some(role => route.meta.roles.indexOf(role) >= 0)) {
        return false
      }
    }
    if (route.meta.isStaff) {
      return parseInt(isStaff) < 1
    }
    if (route.meta.isCompany) {
      const type = getLoginType()
      if (type == null || type == 1) {
        return false
      }
    }
    if (route.meta.isAdmin) {
      const type = getLoginType()
      if (type == null || type == 0) {
        return false
      }
    }
    if (route.meta.enableMallModule) {
      return enableModules.enableMallModule == 1
    }
    if (route.meta.enableDatumModule) {
      return enableModules.enableDatumModule == 1
    }
    if (route.meta.enableSocialGroup) {
      return enableModules.enableSocialGroup == 1
    }
    if (route.meta.enableColumnModule) {
      return enableModules.enableColumnModule == 1
    }
    if (route.meta.enableActivityModule) {
      return enableModules.enableActivityModule == 1
    }
    if (route.meta.enableResourceModule) {
      return enableModules.enableResourceModule == 1
    }
    if (route.meta.enableExamModule) {
      return enableModules.enableExamModule == 1
    }
    if (route.meta.enableWxapp) {
      return enableModules.enableWxapp == 1
    }
    if (route.meta.enablePoints) {
      return enableModules.enablePoints == 1
    }
    if (route.meta.enableAgentLevels) {
      return enableModules.agentLevels != 0
    }
    if (route.meta.isMarket) {
      return enableModules.enablePoints != 0 || enableModules.agentLevels != 0
    }
  }

  return true
}

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param asyncRouterMap
 * @param roles
 */
function filterAsyncRouter(asyncRouterMap, roles, enableModules, staff) {
  const accessedRouters = asyncRouterMap.filter(route => {
    if (hasPermission(roles, route, enableModules, staff)) {
      if (route.children && route.children.length) {
        route.children = filterAsyncRouter(route.children, roles, enableModules, staff)
      }
      return true
    }
    return false
  })
  return accessedRouters
}

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: [],
    enableModules: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ state, commit, rootState }, data) {
      return new Promise(resolve => {
        const { roles, staff, permission } = data
        let accessedRouters
        if (roles.indexOf('admin') >= 0) {
          accessedRouters = asyncRouterMap
        } else {
          accessedRouters = filterAsyncRouter(asyncRouterMap, roles, rootState.user.enableModules, rootState.user.staff)
          /* if (staff == 1) {
            accessedRouters = [];
            for (let i=0; i<permission.length; i++) {
              let pfName = permission[i].name;
              let arr = [];
              for (let m=0;m<asyncRouterMap.length;m++) {
                if (pfName == asyncRouterMap[m].path) {
                  arr.push(asyncRouterMap[m]);
                  let arrChildren = [];
                  for (let j=0; j<permission[i].children.length; j++) {
                    let cfName = permission[i].children.name
                    for (let n = 0; n<asyncRouterMap[m].children.length; n++) {
                      if(cfName == asyncRouterMap[m].children[n].path) {
                        arrChildren.push(asyncRouterMap[m].children[n]);
                      }
                    }
                  }

                }
              }

            }

          }else {
            accessedRouters = filterAsyncRouter(asyncRouterMap, roles, rootState.user.enableModules)
          }*/
        }
        commit('SET_ROUTERS', accessedRouters)
        resolve()
      })
    }
  }
}

export default permission
